/*
 * FormComponent.java
 *
 * Created on May 18, 2007, 5:07 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package info.javatech.jmf.components.form.server;

import info.javatech.jmf.common.components.FormConstants;
import info.javatech.jmf.components.root.server.RootComponent;
import info.javatech.jmf.server.PCommandOwner;
import info.javatech.jmf.server.PComponentHolder;
import info.javatech.jmf.server.PItemOwner;
import info.javatech.jmf.server.protocol.PCommand;
import info.javatech.jmf.server.protocol.PComponent;
import info.javatech.jmf.server.protocol.PItem;
import info.javatech.jmf.server.protocol.PRequest;
import info.javatech.jmf.server.protocol.PScreenContainer;
import info.javatech.jmf.server.util.EncodeUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.FacesEvent;
import javax.faces.event.FacesListener;
import javax.faces.render.Renderer;
import org.apache.commons.lang.StringEscapeUtils;
import javax.faces.component.UIOutput;

/**
 *
 * @author root
 */
public class FormComponent extends UIComponentBase implements PComponentHolder, PCommandOwner,PItemOwner{
    
    private List<PCommand> pcommands;
    
    private List<PItem> pitems;
    
    /** Creates a new instance of FormComponent */
    public FormComponent() {
        pcommands = new LinkedList<PCommand>();
        pitems = new LinkedList<PItem>();
    }
    
    public String getFamily() {
        return "jmf.form";
    }
    
    public void encodeBegin(FacesContext context) throws IOException {
        if (getParent() instanceof RootComponent) {
            this.resetCommandsContainer();
            this.resetItemsContainer();
        } else throw new FacesException("Form component must be the direct child of UIViewRoot");
    }
    
    public void encodeEnd(FacesContext context) throws IOException {
        PScreenContainer container = (PScreenContainer) this.getPComponent();
        
        if (getParent() instanceof RootComponent) {
            ((RootComponent)getParent()).setPScreenContainer(container);
        } else throw new FacesException("FormComponent must have RootComponent as a parent component");
    }
    
    public PComponent getPComponent() {
        PScreenContainer form = new PScreenContainer();
        PCommand[] commands = new PCommand[pcommands.size()];
        int i = 0;
        for(PCommand command:pcommands)
            commands[i++] = command;
        form.setCommands(commands);
        
        i = 0;
        PItem[] items = new PItem[pitems.size()];
        for(PItem item:pitems)
            items[i++] = item;
        form.setItems(items);
        
        form.setPropertyNames(new String[]{FormConstants.TITLE_PROPERTY_NAME, FormConstants.TICKER_PROPERTY_NAME});
        form.setPropertyValues(new String[]{(String)this.getAttributes().get(FormConstants.TITLE_PROPERTY_NAME),(String)this.getAttributes().get(FormConstants.TICKER_PROPERTY_NAME)});
        
        form.setType(FormConstants.TYPE_NAME);
        
        form.setId(this.getId());
        
        return form;
    }
    
    public void addCommand(PCommand command) {
        pcommands.add(command);
    }
    
    public void addItem(PItem item) {
        pitems.add(item);
    }
    
    public void resetCommandsContainer() {
        pcommands.clear();
    }
    
    public void resetItemsContainer() {
        pitems.clear();
    }
    
    
}
